package com.hec.yygh.cmn.controller;


import com.hec.yygh.cmn.service.DictService;
import com.hec.yygh.common.result.R;
import com.hec.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author hec
 * @since 2022-10-14
 */

@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    @PostMapping("/importData")
    public R importData(MultipartFile file){
        dictService.importData(file);
        return R.ok().code(200);
    }

    //导出excel
    @GetMapping("/exportData")
    public void exportExcel(HttpServletResponse response){
        dictService.exportExcel(response);
    }

    @GetMapping("/findChildData/{parentId}")
    public R getDictListByPid(@PathVariable("parentId")Integer parentId){
       List<Dict> dictList = dictService.getDictListByPid(parentId);
       return R.ok().data("list",dictList);
    }

    //根据dit_code查询所有省份信息
    @GetMapping("/findByDictCode/{dictCode}")
    public R getAllProvincesByDictcode(@PathVariable("dictCode") String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }
}

